package com.bankcomm.iccard;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Calendar;
import org.json.JSONException;
import org.json.JSONObject;

import com.bankcomm.R;

import android.app.Activity;

public class CardHandlerUtils {
	/**
	 * 字符串转换成十六进制字符串
	 * 
	 * @throws UnsupportedEncodingException
	 * 
	 * 
	 */
	public static String str2HexStr(String str)
			throws UnsupportedEncodingException {
		char[] chars = "0123456789ABCDEF".toCharArray();
		StringBuilder sb = new StringBuilder("");
		byte[] bs = str.getBytes("utf-8");
		int bit;

		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(chars[bit]);
			bit = bs[i] & 0x0f;
			sb.append(chars[bit]);
			sb.append(' ');
		}
		return sb.toString().trim();
	}

	/**
	 * 十六进制转换字符串
	 * 
	 * @param String
	 *            str Byte字符串(Byte之间无分隔符 如:[616C6B])
	 * @return String 对应的字符串
	 * @throws UnsupportedEncodingException
	 */
	public static String hexStr2Str(String hexStr)
			throws UnsupportedEncodingException {
		String str = "0123456789ABCDEF";
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];
		int n;

		for (int i = 0; i < bytes.length; i++) {
			n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 0xff);
		}
		return new String(bytes, "utf-8").trim();
	}

	// 交易类型：圈存60 消费00
	public static String handlerDealType(Activity act,String typeNum) {
		String s = "";
		JSONObject jo = null;
		try {
			jo = new JSONObject(act.getString(R.string.jsonTypeAerry));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			s=jo.getString(typeNum);
		} catch (Exception e) {
			s=act.getString(R.string.unknownstr);
		}
		return s;

	}

	// 处理金额
	public static String handlerDateFormat(String date) throws ParseException {
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		String str = String.valueOf(year);
		String yearBegin = str.substring(0, 2);
		String dateNeedFormat = yearBegin.concat(date);
		String formatDate = dateNeedFormat.substring(0, 4) + "-"
				+ dateNeedFormat.substring(4, 6) + "-"
				+ dateNeedFormat.substring(6, 8);
		return formatDate;
	}

	// 12位转浮点
	public static String handler12toDecimal(String number12)
			throws ParseException {
		int number=Integer.parseInt(number12);
		float numberf=number/100.0f;
		String decimalNum=String.format("%.2f",numberf);
		return decimalNum;
	}
	// 小数转12位字符串
	public static String handlerDecimalto12(String number) {
		float numberf = Float.parseFloat(number);
		int numberInt = (int) (numberf * 100f);
		String number1 = String.valueOf(numberInt);
		int len = number1.length();
		if (len < 12) {
			while (len < 12) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(number1);// 左补0
				// sb.append(str).append("0");//右补0
				number1 = sb.toString();
				len = number1.length();
			}
		}
		return number1;
	}
}
